import { httpTokenRequest } from '@/utils/http.js';
export default {
	data() {
		var checkMobile = (rule, value, callback) => {
			if (!value) {
				return callback(new Error('请输入手机号'));
			}
			var reg = /^1[3456789]\d{9}$/;
			if (!reg.test(value)) {
				return callback(new Error('请输入正确的手机号'));
			} else {
				return callback();
			}
		};
		return {
			rules: {
				mobile: [
					{
						validator: checkMobile,
						trigger: 'blur'
					}
				]
			},
			form: {
				mobile: '', //手机号
				captcha: '', //验证码
				app_version: '' //版本号
			},
			checked: false,
			isShow: false
		};
	},
	methods: {

		isConfirm() {
			this.checked = true;
			this.isShow = false;
			this.sendCode('form')
		},
		//回到上一页
		goBack() {
			let page = getCurrentPages();
			if (page.length == 1) {
				uni.switchTab({
					url: '/pages/index/index'
				});
			} else {
				uni.navigateBack({
					delta: 1
				});
			}
		},
		//点击复选框
		onCheck(e) {
			this.checked = !this.checked;
		},
		async sendCode(formName) {
			try {
				await this.$refs[formName].validate().then(valid => {
					if (!valid) {
						console.log('error submit!!');
						throw new Error('Form validation failed');
					}
				});
			} catch (error) {
				// 处理错误
				console.error(error);
				// 终止整个代码的执行
				return;
			}
			if (!this.checked) {
				this.isShow = true
				return;
			}

			var data = {
				mobile: this.form.mobile, //	是	string	手机号
				event: 'mobilelogin' //是	string	事件:register=注册,mobilelogin=验证码登录,changemobile=更改手机号,resetpwd=找回密码
			};

			httpTokenRequest({
				url: this.$paths.smssend,
				method: 'POST',
				params: data
			}).then((res) => {
				if (res.code == 1) {
					this.$tools.showToast('短信发送成功');
					setTimeout(() => {
						uni.navigateTo({
							url: './checkCode?tel=' + this.form.mobile
						});
					}, 800);
				} else {
					this.$tools.showToast(res.msg);
				}
			})
				.catch((err) => {
					console.log('错误回调', err);
				});
		},
		jump(url) {
			uni.navigateTo({
				url: url
			})
		}
	},
	onLoad(e) {
		if (e.tel) {
			this.form.mobile = e.tel;
		}
	},
	onShow() {

	},
};